package gulimall;


import java.util.concurrent.TimeUnit;

/**
 * 可重入锁演示
 *
 * @author HLF
 * @date 2022/7/6 9:59
 */
public class ReentrantLock {
    public static void main(String[] args) {
        MyThread myThread = new MyThread();

        new Thread(myThread::goHome,"张三").start();

        new Thread(myThread::goHome,"李四").start();
    }
}


class MyThread {

    /**
     * 进入家里
     */
    public synchronized void goHome() {
        String name = Thread.currentThread().getName();

        System.out.println(name + "进入了家里！");
        bedroom();
        balcony();

    }

    /**
     * 进入家里的卧室
     */
    public synchronized void bedroom() {
        String name = Thread.currentThread().getName();

        System.out.println(name + "进入到家里的卧室");
        try {
            TimeUnit.SECONDS.sleep(2L);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println(name + "从家里的卧室出来了");
    }

    public synchronized void balcony() {
        String name = Thread.currentThread().getName();

        System.out.println(name + "进入到家里的阳台");
        try {
            TimeUnit.SECONDS.sleep(2L);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println(name + "从家里的阳台出来");
    }
}
